package com.zhipu.resetbalance.utils;

import android.os.Environment;

import java.text.DecimalFormat;
import java.util.Locale;

public class CommonUtils {




    /**
     * 签名
     * @param mJson
     * @return
     */
    public static String toHexString(String mJson){
        byte[] byteSign = InitializeUtils.getSignV0().signatureParams(mJson);
        return toHexString(byteSign);
    }

    public static String toHexString(byte[] byteArray) {
        StringBuilder sb = new StringBuilder();
        for (byte b : byteArray) {
            String hex = Integer.toHexString((b & 0xFF) | 0x100);
            sb.append(hex.substring(1, 3));
        }
        return sb.toString().toUpperCase(Locale.ROOT);
    }






    /**
     * 名称
     * @return
     */
    public static String getApkName(){
        String apkUrl = Environment.getExternalStorageDirectory().getAbsolutePath();
        String appName = apkUrl.substring(apkUrl.lastIndexOf("/") + 1, apkUrl.length());
        if (!appName.endsWith(".apk")) {
            appName = "Balance.apk";
        }
        return appName;
    }



    /**
     * 保留两位小数，四舍五入的一个老土的方法
     * @param d
     * @return
     */
    public static double formatDouble(Double d) {
        if (d == null || d.toString().equals("Infinity") || d.toString().equals("-Infinity") || d.toString().equals("NaN")) {
            return 0.00;
        } else {
            DecimalFormat mDecimalFormat = new DecimalFormat("#.00");
            String calculation = mDecimalFormat.format(d);
            return Double.parseDouble(calculation);
        }
    }
}
